var api = require('../../utils/api.js');
var app = getApp();
import jiuaiDebounce from '../../utils/jiuai-debounce.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderDetail: '',

    modalName: null,
    current: '',
    urlsArray: [],
    hid: 0
  },
  expressInfo() {
    wx.navigateTo({
      url: '/pages/expressInfo/expressInfo?bid=' + this.data.b_id,
    })
  },

  //退换货
  returnGoods(e) {
    let hid = e.currentTarget.dataset.hid;
    this.setData({
      hid
    })
    if (!this.data.current) {
      wx.navigateTo({
        url: '/pages/returnGoods/returnGoods?bid=' + this.data.b_id + '&status=' + this.data.orderDetail.status
      })
      return
    }
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
    // wx.navigateTo({
    //   url: '/pages/returnGoods/returnGoods?bid='+this.data.b_id,
    // })
  },

  //联系卖家
  concatNumber(e) {
    // let phoneNumber = e.currentTarget.dataset.mobile;
    // wx.makePhoneCall({

    //   phoneNumber

    // })
    let hid = e.currentTarget.dataset.hid
    this.setData({
      modalName: e.currentTarget.dataset.target,
      hid
    })
  },
  //申请退款
  refund(e) {
    let hid = e.currentTarget.dataset.hid;
    this.setData({
      hid
    })
    if (!this.data.current) {
      wx.navigateTo({
        url: '/pages/returnGoods/returnGoods?bid=' + this.data.b_id + '&status=' + this.data.orderDetail.status
      })
      return
    }
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  //客服二维码预览
  preImage() {
    wx.previewImage({
      current: this.data.current, // 当前显示图片的http链接
      urls: this.data.urlsArray // 需要预览的图片http链接列表
    })
  },

  //跳转填写退货表单
  closeModal() {
    if (this.data.hid == 1) { return }
    wx.navigateTo({
      url: '/pages/returnGoods/returnGoods?bid=' + this.data.b_id + '&status=' + this.data.orderDetail.status
    })
  },
  //立即付款
  payNow(e) {
    jiuaiDebounce.canDoFunction({
      key: "tap",//基于此值判断是否可以操作，如两个方法传入了同样的key，则会混淆，建议传入调用此事件的方法名，简单好梳理
      time: 5000,//如果传入time字段，则为定时器后，自动解除锁定状态，单位（毫秒）
      success: () => {//成功中调用应该操作的方法，被锁定状态不会执行此代码块内的方法
        wx.showLoading({
          mask: true
        })
        let b_id = e.currentTarget.dataset.bid;
        console.log(b_id)

        let params = {
          token: wx.getStorageSync('token'),
          b_id
        }
        api.fetchApi('/Payment/once_pay_order', params).then(res => {
          let { nonceStr, package: package1, paySign, signType, timeStamp } = res;
          wx.requestPayment({
            timeStamp,
            nonceStr,
            package: package1,
            signType,
            paySign,
            success: (req) => {
              wx.showToast({
                title: '支付成功',
              })
              setTimeout(() => {

                wx.navigateTo({
                  url: '/pages/myOrder/myOrder?page=FillOrder',
                })
              }, 500)
            },
            fail: (err) => {
              wx.showToast({
                title: '支付失败',
                icon: 'none'
              })

            }
          })
        })
      }
    })


  },

  //取消订单
  deleteOrder(e) {
    let id = e.currentTarget.dataset.bid;
    wx.showModal({
      title: '提示',
      content: '确定取消订单?',
      success: (res1) => {
        if (res1.confirm) {
          api.fetchApi('/order/del_order', { token: wx.getStorageSync('token'), id }).then(res => {
            wx.showToast({
              title: '取消成功'
            })
            setTimeout(() => {
              var pages = getCurrentPages();//页面指针数组
              var prepage = pages[pages.length - 2];//上一页面指针
              prepage.getOrderList(1, 0, '');
              wx.navigateBack({

              })
            }, 1000)
          })
        }
      }
    })


  },
  //订单详情
  getOrderDetail() {
    wx.showLoading({
      title: '加载中',
    })
    let params = {
      token: wx.getStorageSync('token'),
      b_id: this.data.b_id
    }
    api.fetchApi('/order/order_detail', params).then(res => {
      wx.hideLoading();
      let orderDetail = res;
      let current = res.wx_img.wx_img || '';
      this.setData({
        orderDetail,
        current,
        urlsArray: [current]
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let { b_id } = options;
    this.setData({
      b_id
    })
    this.getOrderDetail();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})